package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.CompanyMapper;
import com.it.mapper.DeptMapper;
import com.it.mapper.UserInfoMapper;
import com.it.mapper.UserMapper;
import com.it.pojo.*;
import com.it.pojo.vo.UserVo;
import com.it.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/13 16:39
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public List<UserVo> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findNameAndPwd(String username, String password) {
        User user = userMapper.findNameAndPwd(username, password);
        return user;
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int insertUser(User user) {
        user.setUserId(getPrimaryKey());
        user.setUserLogintime(new Date());
        UserInfo userInfo = new UserInfo();
        userInfo.setInfoId(user.getUserId());
        userInfo.setUserId(user.getUserId());
        int i = userMapper.insertSelective(user);
        userInfoMapper.insertSelective(userInfo);
        return i;
    }

    private String getPrimaryKey(){
        UserExample example = new UserExample();
        List<User> list = userMapper.selectByExample(example);
        return list.size()+1+"";
    }


    @Override
    public int updateById(String userId,String infoId) {
        User user = userMapper.selectByPrimaryKey(userId);
        //假删除 0:显示  1:删除
        user.setUserDel(1);
        UserInfo info = userInfoMapper.selectByPrimaryKey(infoId);
        info.setInfoDel(1);
        userInfoMapper.updateByPrimaryKeySelective(info);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<UserVo> findPage(Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<UserVo> list = findAll();
        PageInfo<UserVo> pageInfo = new PageInfo<>(list,5);
        return pageInfo;
    }

    @Override
    public UserVo findById(String userId) {
        return userMapper.findById(userId);
    }

    @Override
    public int updateUserInfo(UserVo vo) {
        System.out.println(vo);
        User user = new User();
        BeanUtils.copyProperties(vo,user);
        UserInfo info = new UserInfo();
        BeanUtils.copyProperties(vo,info);
        Dept dept = deptMapper.selectByPrimaryKey(info.getDeptId());
        info.setDeptName(dept.getDeptName());
        /*Company company = companyMapper.selectByPrimaryKey(info.getCompanyId());
        info.setCompanyName(company.getCompanyName());*/
        userMapper.updateByPrimaryKeySelective(user);
        return userInfoMapper.updateByPrimaryKeySelective(info);
    }

    @Override
    public User findEditById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public User findByName(String loginName) {
        return userMapper.findByName(loginName);
    }

    @Override
    public List<User> queryAll() {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserDelEqualTo(0);//未删除
        criteria.andUserStatusEqualTo(0);//启用用户
        return userMapper.selectByExample(example);
    }


}
